package ouhenry.stage.common.base;

import java.time.LocalDateTime;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 
 * @Description 返回页面结果集
 * @author Ou Henry
 * @Date 2022-02-13 11:59:58
 */
@ApiModel(description = "rest请求的返回模型，所有rest正常都返回该类的对象")
public class Result<T> {

    public static final Integer SUCCESS_CODE = 200;
    public static final String SUCCESS_MSG = "处理成功";

    @ApiModelProperty(value = "处理结果code", required = true)
    private Integer code;

    @ApiModelProperty(value = "处理结果描述信息")
    private String message;

    @ApiModelProperty(value = "请求结果生成时间戳")
    private LocalDateTime time;

    @ApiModelProperty(value = "处理结果数据信息")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public LocalDateTime getTime() {
        return time;
    }

    public void setTime(LocalDateTime time) {
        this.time = time;
    }

    public T getData() {
        return data;
    }

    public Result() {
        this.time = LocalDateTime.now();
    }

    /**
     * @param errorEnum
     */
    public Result(ErrorEnum errorEnum) {
        this.code = errorEnum.getCode();
        this.message = errorEnum.getMsg();
        this.time = LocalDateTime.now();
    }

    /**
     * @param errorEnum
     * @param data
     */
    public Result(ErrorEnum errorEnum, T data) {
        this(errorEnum);
        this.data = data;
    }

    private Result(Integer code, String msg) {
        this.code = code;
        this.message = msg;
        this.time = LocalDateTime.now();
    }

    /**
     * 快速创建成功结果并返回结果数据
     * 
     * @param <T>
     * @param data
     * @return
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<T>(SUCCESS_CODE, SUCCESS_MSG);
        result.setData(data);
        return result;
    }

    /**
     * 快速创建成功结果
     *
     * @return Result
     */
    public static Result<?> success() {
        return success(null);
    }

    /**
     * 系统异常类并返回结果数据
     *
     * @param errorEnum
     * @param data
     * @return Result
     */
    public static <T> Result<T> fail(ErrorEnum errorEnum, T data) {
        return new Result<T>(errorEnum, data);
    }

    /**
     * 系统异常类并返回结果数据
     *
     * @param errorEnum
     * @return Result
     */
    public static <T> Result<T> fail(ErrorEnum errorEnum) {
        return Result.fail(errorEnum, null);
    }

    /**
     * 成功code=000000
     *
     * @return true/false
     */
    @JsonIgnore
    public boolean isSuccess() {
        return SUCCESS_CODE.equals(this.code);
    }

    /**
     * 失败
     *
     * @return true/false
     */
    @JsonIgnore
    public boolean isFail() {
        return !isSuccess();
    }
}
